$(function(){



    $("#update-btn").click(function (){
        edit();
    });


    if(food_id.trim() !== ""){
        fetchFood1();

    }
    $("#kindCombo").change(function () {
        fetchFood();
    })


    $("#foodCombo").change(function () {
        $(":input[name = food_id]").val($(this).val());
    });
    bur();


});
function bur(){
    $(":input[name = food_id]").blur(function () {
        let text = $(":input[name = food_id]").val();
        let $option = $("#foodCombo").children("[name=" + text + "]")
        if ($option.length == 0) {
            layer.msg("该分类编号不存在");
            return;
        }
        $option.prop("selected", true);
    });
}
function edit(){

    let empId = id;
    let emp_id =$(":input[name = emp_id]").val();
    if(emp_id.trim() === ""){
        layer.msg("顾客编号不可为空");
        return false;
    }
    let ord_id = $(":input[name= ord_id]").val();
    if(ord_id.trim() === ""){
        layer.msg("订单编号不可为空");
        return  false;
    }
    let food_id = $(":input[name= food_id]").val();
    if(food_id.trim() === ""){
        layer.msg("食品编号不可为空");
        return  false;
    }
    let text = $(":input[name = food_id]").val();
    let $option = $("#foodCombo").children("[name=" + text + "]")
    if ($option.length == 0) {
        layer.msg("该分类编号不存在");
        return;
    }
    let url = ctx + "/order/edit";
    console.log(empId);
    $.post(url,{
        empId,
       emp_id,
        ord_id,
        food_id
    },function (resp){
        if(resp.success){
            layer.alert("修改成功",function (){
                location.href = ctx+"/order/list";
            });
        }else{
            layer.alert(resp.error||"删除失败，请稍后再试");
        }

    },"json");
}

function fetchKind(){

    let url = ctx + "/kind/comboList";
    $.post(url,{},function(resp){
        let klist = resp.klist;
        $("#kindCombo").empty();
        let k = $("#foodCombo>option:selected");
        klist.forEach(function (item){
            if(k.val() == item.kindId){
                console.log(k.val());
                $("#kindCombo").append($("<option value='"+item.kindId+"' name='"+item.kindId+"' selected='selected'>"+item.name+"</option>"));
            }
            else{
                $("#kindCombo").append($("<option value='"+item.kindId+"' name='"+item.kindId+"'>"+item.name+"</option>"));
            }
        });
    });

}
function fetchFood(){
    // layer.msg("触发");

    let url = ctx + "/food/comboList";
    $.post(url,{},function(resp){
        let flist = resp.flist;
        $("#foodCombo").empty();
        let k = $("#kindCombo>option:selected");
        console.log(k.get(0));
        let index=0;
        flist.forEach(function (item){
            if(k.val() == item.kindId){
                if(index === 0){
                    $(":input[name = food_id]").val(item.foodId);
                    index++;
                }
                $("#foodCombo").append($("<option value='"+item.foodId+"' name='"+item.foodId+"'>"+item.name+"</option>"));
            }
        });
    });

}
function fetchFood1(){
    // layer.msg("触发");

    let url = ctx + "/food/comboList";
    $.post(url,{},function(resp){
        let flist = resp.flist;
        $("#foodCombo").empty();
        flist.forEach(function (item){
            if(food_id == item.foodId){
                $("#foodCombo").append($("<option value='"+item.foodId+"' name='"+item.foodId+"' selected='selected'>"+item.name+"</option>"));
            }else {
                $("#foodCombo").append($("<option value='"+item.foodId+"' name='"+item.foodId+"'>"+item.name+"</option>"));
            }
        });
        fetchKind();
    });

}